<template>
  <div class="user-container">
    <!-- 用户信息面板 -->
    <div class="user-card">
      <!-- 用户头像，昵称 -->
      <van-cell>
        <template #icon>
          <img class="user-photo" :src="user.photo" alt="头像">
        </template>
        <template #title>
          <span class="username">{{user.name}}</span>
        </template>
        <template #label>
          <van-tag color="#fff" text-color="#007bff">lv0</van-tag>
        </template>
      </van-cell>
      <!-- 动态、粉丝、关注 -->
      <div class="user-data">
        <div class="user-data-item">
          <span>{{user.art_count}}</span>
          <span>动态</span>
        </div>
        <div class="user-data-item">
          <span>{{user.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="user-data-item">
          <span>{{user.fans_count}}</span>
          <span>粉丝</span>
        </div>
      </div>
    </div>
    <!-- 操作面板 -->
    <van-cell-group>
      <van-cell title="编辑资料" icon="edit" is-link @click="moveUserEdit"/>
      <van-cell title="小思同学" icon="chat-o" is-link />
      <van-cell title="退出登陆" icon="warning-o" is-link @click="signOut"/>
    </van-cell-group>
  </div>
</template>

<script>
import { userInfoAPI } from '@/api/index'
import { signOutStorage } from '@/utils/storage'
export default {
  name: 'User',
  data () {
    return {
      user: {}
    }
  },
  methods: {
    // 获取用户数据
    async getUserInfo () {
      const res = await userInfoAPI()
      this.user = res.data.data
      // console.log(res.data.data)
    },
    // 退出登陆
    signOut () {
      this.$dialog.confirm({
        title: '退出',
        message: '真的要走了吗？'
      })
        .then(() => {
          // on confirm
          // 清空本地保存的token
          signOutStorage()
          // 退出登陆，返回登陆页面
          this.$router.push({
            path: '/login'
          })
        })
        .catch(() => {
          // on cancel
          // 关闭弹窗
          this.$dialog.close()
        })
    },
    // 跳转到编辑资料
    moveUserEdit () {
      this.$router.push({
        path: '/layout/user_edit'
      })
    }
  },
  //  把created换成activated钩子函数
  // created () {
  //   this.getUserInfo()
  // },
  activated () {
    // 使用了缓存，如果编辑资料修改头像后再切换回用户信息页面，更新头像
    this.getUserInfo()
  }
}
</script>

<style lang='less' scoped>
.user-container {
  .user-card {
    background-color: #007bff;
    color: white;
    padding-top: 20px;
    .van-cell {
      background: #007bff;
      color: white;
      &::after {
        display: none;
      }
      .user-photo {
        width: 60px;
        height: 60px;
        background-color: #fff;
        border-radius: 50%;
        margin-right: 10px;
      }
      .username {
        font-size: 14px;
        font-weight: bold;
      }
    }
  }
    .user-data {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    font-size: 14px;
    padding: 30px 0;
    .user-data-item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 33.33%;
    }
  }
}
</style>
